package weeks;

/**
 * Weeks - Visualisation, amalgamates all of the information gathered by the other objects
 * and displays it in an ambient fasion
 * @author - Owen Phelan
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import preferences.Splash;
import processing.core.PApplet;
import processing.core.PImage;
import processing.opengl.PGraphicsOpenGL;

import javax.bluetooth.BluetoothStateException;
import javax.swing.JOptionPane;
import bluetooth.*;

import com.google.gdata.util.AuthenticationException;


public class weeks extends PApplet {

	PImage cloud;
	PImage busStop;
	PImage sun;
	PImage moon;
	PImage[] characters;
	

	public int numOfEvents;
	public int numOfCals;
	public int numOfBlogs;
	public String username;
	public boolean useBluetooth;
	public List<DeviceDetails> nearbyusers = new ArrayList<DeviceDetails>();
	
	/**public static void main(String[] args) {
		PApplet.main(new String[] { "--present", "FullScreenApp" });
	}**/
	
	public weeks(int numOfCals, int numOfEvents, String username, boolean useBluetooth){
		this.numOfCals = numOfCals;
		this.numOfEvents = numOfEvents;
		this.username = username;
		this.useBluetooth = useBluetooth;
	}

	/**
	 * Bluetooth Scanning method, will run while the viz is 
	 * running if it is enabled
	 * @return list
	 * @throws BluetoothStateException
	 */
	public List<DeviceDetails> scanBluetooth() throws BluetoothStateException {
		while(useBluetooth = true) {
			BluetoothSpotter bs = new BluetoothSpotter();
			nearbyusers = bs.performScan();
		}
		return nearbyusers;
	}
	
	/**
	 * setup the window to be based on the resolution of the screen it is on
	 * @throws IOException 
	 * @throws AuthenticationException 
	 */
	public void setup() {
		// initally a WVGA Display
		size(screen.width, screen.height, P3D);
		frameRate(1);
		
		background(22, 44, 22);
		System.out.println("-------------------------------");
		System.out.println("Details for user: " + username);
		System.out.println("-------------------------------");
		System.out.println("Number of events: " + numOfEvents);
		System.out.println("Number of calendars: " + numOfCals);
		System.out.println("Number of blogs: " + numOfBlogs);
		System.out.println();
	}


	/**
	 * Draw the road and the pavement
	 */
	public void drawGround() {
		// road
		pushMatrix();
		translate(0, height - 100);
		beginShape();
		fill(80);
		rect(0, 0, width, 100);
		endShape();
		popMatrix();

		// snow on the road
		pushMatrix();
		translate(0, height - 150);
		fill(250);
		rect(0, 0, width, 50);

		pushMatrix();
		translate(0, height - 75);
		fill(400);
		rect(0, 0, width, 5);

		popMatrix();
		popMatrix();
	}

	/**
	 * Draw the bus stop TODO: make the bus-stop change with differing bus times
	 * looking at dapper scraper, as Dublin Bus dont publish times via xml
	 */
	public void drawBusStop() {

		busStop = loadImage("Weeks/data/normalBStop.png");

		// draw post
		stroke(50);
		pushMatrix();
		translate(50, height - 240);
		fill(10);
		rect(0, 0, 10, 125);
		popMatrix();

		// yellow diamond stop sign
		pushMatrix();
		image(busStop, 24, height - 240);
		popMatrix();
	}

	/**
	 * Draw mountains in Background TODO: make the coordinates relative to
	 * height and width and make them mean something!
	 */
	public void drawMountains() {

		stroke(300);

		pushMatrix();
		translate(-100, 80);
		fill(60, 100, 60);
		triangle(670, height - 150, 220, 60, -500, height - 150);
		popMatrix();

		pushMatrix();
		translate(600, 100);
		fill(60, 100, 60);
		triangle(670, height - 150, 220, 60, -300, height - 150);
		popMatrix();

		pushMatrix();
		translate(50, 100);
		fill(30, 70, 45);
		triangle(670, height - 150, 220, 60, -500, height - 150);
		popMatrix();

		pushMatrix();
		translate(-400, 100);
		fill(40, 80, 60);
		triangle(670, height - 150, 220, 60, -500, height - 150);
		popMatrix();
	}

	/**
	 * This makes a different background for five major periods of a day Paint
	 * the background which changes color with time (hour() and background() are
	 * methods within core.jar)
	 */
	public void drawSky() {

		sun = loadImage("Weeks/data/sun.png");
		moon = loadImage("Weeks/data/moon.png");

		// 00:00 - 06:00 -> Purple
		if ((hour() >= 0) && (hour() <= 6)) {
			background(180, 32, 246);
			image(moon, width - 200, height / 6);
		}

		// 06:00 - 12:00 -> Blue
		if ((hour() >= 6) && (hour() <= 12)) {
			background(90, 147, 253);
			image(sun, width - 200, height / 6);
		}

		// 12:00 - 17:00 -> Green
		if ((hour() >= 12) && (hour() <= 17)) {
			background(107, 167, 169);
			image(sun, width - 200, height / 6);
		}

		// 17:00 - 21:00 -> Yellow
		if ((hour() >= 17) && (hour() <= 21)) {
			background(240, 215, 100);
			image(moon, width - 200, height / 6);
		}

		// 21:00 - 23:00 -> Black
		if ((hour() >= 21) && (hour() <= 23)) {
			background(0, 0, 0);
			image(moon, width - 200, height / 6);
		}
	}

	/**
	 * Calculate and Draw the clouds: This gets the number of events in your
	 * calendar and draws that number of clouds in the sky, in a pseudorandom
	 * fashion
	 */

	public void drawClouds() {

		cloud = loadImage("Weeks/data/cloud.png");

		// a,b,c: so the clouds can be randomly distributed

		// draw clouds for each event
		for (int i = 0; i <= numOfEvents - 1; i++) {
			float a = random(50, 100);
			for (int j = 0; j <= numOfEvents; j++) {
				image(cloud, i * 120, a);
			}
		}

	}

	/**
	 * Show the people within the vicinity TODO: implement Character.java and
	 * ContextComms.java so the Character will be generated using data from the
	 * latter
	 */
	public void drawPeople() {
		PImage mycharacter = loadImage("Weeks/data/1.png");
		PImage characterTwo = loadImage("Weeks/data/2.png");
		PImage characterThree = loadImage("Weeks/data/3.png");
		PImage characterFour = loadImage("Weeks/data/4.png");
		
		if (username == "visualcalendar") {
			//mycharacter.filter(BLUR, (float) 5.4);
			image(mycharacter, 75, height - 250);
		}
		
		if (nearbyusers.contains("Nokia 6120 classic")) {
			image(characterTwo, 250, height - 250);
		}
		
		for(int i = 0; i<=nearbyusers.size()-1; i++){
			System.out.println(nearbyusers.get(i));
		}
		
		image(characterThree, 400, height - 250);
		image(characterFour, 550, height - 250);
	}

	/*
	 * Draw the trees behind the people and in front of the mountains -
	 * represents the calendars
	 */
	public void drawTrees() {
		PImage tree = loadImage("Weeks/data/tree.png");
		
		for (int i = 0; i <= numOfCals; i++) {
			image(tree, 100 + i * 150, height - 325);
		}
	}

	/**
	 * Tells the coordinates of the mouse, easier to tell where to put stuff
	 */
	public void printCoords() {
		System.out.println("(" + mouseX + "," + mouseY + ")");
	}

	/**
	 * Sort of the main method in Processing, but not really. These are drawn
	 * many times a second!
	 */
	@Override
	public void draw() {
		drawSky();
		drawMountains();
		drawClouds();
		drawGround();
		drawBusStop();
		printCoords();
		drawTrees();
		drawPeople();
	}

}